package com.turbo.cloud.contract.utils;

import com.turbo.cloud.starter.sequence.RedisIncrSequenceFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
@RequiredArgsConstructor
public class SequenceNoGenerateUtil {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");

    final RedisIncrSequenceFactory redisIncrSequenceFactory;

    @Value("${contract-application.sequence-no.contract-template-prefix}")
    private String contractTemplatePrefix;

    /**
     * 生成合同模板编号
     * */
    public String generateContractTemplateSequenceNo() {
        String today = LocalDate.now().format(FORMATTER);
        String sequenceNoByDaily = contractTemplatePrefix + today;
        return sequenceNoByDaily + redisIncrSequenceFactory.nextValueByDaily(contractTemplatePrefix, 4);
    }

}
